var webpack = require('webpack');
var path = require('path');
var glob = require('glob');
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CleanWebpackPlugin = require('clean-webpack-plugin');
var config = {
    cache: true,
    //页面入口文件配置
    entry:getEntry(),
    //入口文件输出配置
    output: {
        path: path.join(__dirname, '/web'),
        publicPath: "web/js/",
        filename: '[name].js'
    },

    //其它解决方案配置
    resolve: {
        alias: {
            //插件名称
            zepto:__dirname+'/static/lib/zepto/zepto.min.js',
            utils:__dirname+'/static/lib/utils.js',
            swiper:__dirname+'/static/lib/swiper.min.js',
            common:__dirname+'/static/js/common/common.js',

        }
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader") },
            { test: /\.scss$/, loader: "style!css!sass" },
            { test: /\.less$/, loader: "style!css!less" },
            {
                test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
                loader: 'url-loader?limit=999999' ,
            }
        ]
    },
    //插件项
    plugins: [
        new CommonsChunkPlugin('js/common.js'),
        new ExtractTextPlugin("css/styles.css"),
        new CleanWebpackPlugin(['web'], {
            root: '', // An absolute path for the root  of webpack.config.js
            verbose: true,// Write logs to console.
            dry: false,// Do not delete anything, good for testing.
        })
    ]
};

// 多入口文件
function getEntry(){
    var entrys = {};
    var src = new RegExp(__dirname.replace(/\\/g, "/") + "/static/");
    glob.sync(__dirname + '/static/js/*.js').forEach(function(name) {
        // 前缀
        var entry = name.replace(src, "");
        console.log("entry:"+entry);
        // 后缀
        entry = entry.replace(/\.js$/, "");
        entrys[entry] = name;
    });
    console.log("entrys:"+entrys);
    return entrys;
};

module.exports=config;